/*
 * =====================================================================================
 *
 *       Filename:  main.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015/05/29 09时35分06秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Liu Yi (), swliuyi@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include  <fstream>
#include  <vector>
#include <map>
#include  "dictmatch.h"


using namespace std;
void SplitString(const string& s, vector<std::string>& v, const string& c)
{
    string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while(string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2-pos1));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != s.length())
      v.push_back(s.substr(pos1));
}

void det(map<string, string> &dic, vector<string> &wordList, size_t &start, int maxLen)
{
    if( start >= wordList.size() )
    {
        return;
    }
    for( int i  = start + maxLen; i > start ; i-- )
    {
        string tmp = "";
        if( i <= wordList.size() )
        {
            for( int j = start ; j < i ; j++ )
            {
                tmp += wordList[j];
                tmp += (j + 1== i?"":" ");
                //tmp += " ";
            }
           // cout<<tmp<<endl;
            if( dic.count(tmp) )
            {
            //    cout<<"find"<<endl;
            //    cout<<tmp<<"  "<<entity<<endl;
                string pre = "[";
                pre += dic[tmp];
                pre += " ";
                wordList[start] = pre + wordList[start];
                wordList[i-1] += " ]";
                start = i;
                return;
            }
        }
    }
}

int main(int argc, char *argv[])
{
    if( argc < 6 )
    {
        cout<<"USAGE:./this name org loc input output"<<endl;
        return 1;
    }
    Dict name, org, loc;
    name.LoadDict(argv[1]);
    org.LoadDict(argv[2]);
    loc.LoadDict(argv[3]);
    map<string, string> dictAll;
    set<string>::iterator it;
    for( it = name.GetDict().begin() ; it != name.GetDict().end() ; it++ )
    {
        dictAll[*it] = "nr";
    }
    for( it = loc.GetDict().begin() ; it != loc.GetDict().end() ; it++ )
    {
        dictAll[*it] = "ns";
    }
    for( it = org.GetDict().begin() ; it != org.GetDict().end() ; it++ )
    {
        dictAll[*it] = "nt";
    }
    
    cout<<"load over"<<endl;
    

    ifstream file(argv[4]);
    ofstream output(argv[5]);
    string line;
    while( getline(file, line) )
    {
        vector<string> wordList;
        SplitString(line, wordList, " ");
        //cout<<"!!!"<<wordList[wordList.size()-1]<<endl;
        size_t i = 0;
        while(i < wordList.size())
        {
            size_t j = i;
            det(dictAll,  wordList, i, 7);
            if( i == j ) i++;
        }
        for( size_t j = 0 ; j < wordList.size() ; j++ )
        {
            output<<wordList[j]<<" ";
        }
        output<<endl;
    }
}
